// 定义SSE传输工具函数
export const sseFetch = async (url: string, headers: Record<string, string> = {}) => {
  const options = {
    method: 'GET',
    headers: {
      Accept: 'text/event-stream',
      'Cache-Control': 'no-cache',
      Connection: 'keep-alive',
      ...headers, // 合并用户传入的请求头
    },
  }

  try {
    const response = await fetch(url, options)

    if (!response.ok) {
      throw new Error(`Failed to connect to SSE endpoint: ${response.status} ${response.statusText}`)
    }

    // 不使用流式读取，直接获取完整响应文本
    const data = await response.text()
    return data
  } catch (error) {
    console.error('Error in SSE connection:', error)
    throw error
  }
}
